/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package slsBeans;

import EL.RoomTypes;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.*;

/**
 *
 * @author Tian
 */
@Stateless
public class RoomTypeBLO implements RoomTypeBLORemote {
    
    @PersistenceContext(unitName = "Tunga_EJB3PU")
    private EntityManager em;

    public List<RoomTypes> getAll()
    {
        String hql="FROM RoomTypes";
        Query query = this.em.createQuery(hql);
        List<RoomTypes> roomTypes = query.getResultList();
        return roomTypes;
    }

    public RoomTypes getByID(int id)
    {
        RoomTypes roomType = em.find(RoomTypes.class, id);
        return roomType;
    }

    public boolean add(RoomTypes roomType)
    {
        try
        {
            em.persist(roomType);
            return true;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return false;
        }
    }

    public boolean edit(RoomTypes roomType)
    {
        try
        {
            this.em.merge(roomType);
            return true;
        }
        catch (Exception e)
        {
            e.printStackTrace();
            return false;
        }
    }

    public boolean delete(int id)
    {
        RoomTypes roomType = getByID(id);
        if(roomType != null)
        {
            try
            {
                this.em.remove(roomType);
                return true;
            }
            catch (Exception e)
            {
                e.printStackTrace();
                return false;
            }
        }
        return false;
    }

    public List<RoomTypes> getByType(String type)
    {
        String hql = "FROM RoomTypes as rt WHERE rt.type = ?1 ";
        Query query = this.em.createQuery(hql);
        query.setParameter(1, type);
        List<RoomTypes> rt = query.getResultList();
        return rt;
    }




}
